package org.ewaf.framework.util;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;


public class BeanUtil {
	
	private static final Object[] EMPTY_ARRAY = new Object[0];
	private final static Logger logger = Logger.getLogger(BeanUtil.class);
	@SuppressWarnings("unchecked")
	public static void populateMap(Map map, Object bean) {
		PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(bean.getClass());
		for (int i = 0; i < descriptors.length; i++) {
			PropertyDescriptor dsc = descriptors[i];
			Method method = dsc.getReadMethod();
			if (method == null) {
				continue;
			}
			
			try {
				Object value = method.invoke(bean, EMPTY_ARRAY);
				if (value != null) {
					map.put(dsc.getName(), value);
				}
			} catch (InvocationTargetException e) {
				throw new RuntimeException(e.getTargetException());
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void populateBean(Object bean, Map map) {
		try{
			BeanUtils.populate(bean, map);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e.getTargetException());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	public static <T> List<T> populateList(List<Map<String,Object>> list,Class<T> beanClass){
		List<T> resultList=new ArrayList<T>();
		for(Map<String,Object> map:list){
			try {
				T bean=beanClass.newInstance();
				populateBean(bean, map);
				resultList.add(bean);
			} catch (InstantiationException e) {
				logger.error("转换list出错",e);
				throw new RuntimeException(e);
			} catch (IllegalAccessException e) {
				logger.error("转换list出错",e);
				throw new RuntimeException(e);
			}
		}
		return resultList;
	}
	public static void copyProperties(Object dest, Object orig) {
		try {
			BeanUtils.copyProperties(dest, orig);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e.getTargetException());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Object getProperty(Object bean, String propertyName) {
		try {
			return PropertyUtils.getProperty(bean, propertyName);
		} catch (InvocationTargetException e) {
			throw new RuntimeException(e.getTargetException());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	public static Object getStaticField(String path) {
		int dot = path.lastIndexOf('.');
		String className = path.substring(0, dot);
		String fieldName = path.substring(dot + 1);
		
		try {
			Class<?> clazz = Class.forName(className);
			Field field = clazz.getField(fieldName);
			return field.get(null);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
}
